package com.game.centerserver.common;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.centerserver.pojo.request.BaseRequest;
import com.game.centerserver.utils.HttpServletUtil;
import jakarta.servlet.http.HttpServletRequest;

public class PageFactory {
    private static final String PAGE_SIZE_PARAM_NAME = "pageSize";
    private static final String PAGE_NO_PARAM_NAME = "pageNo";

    public PageFactory() {
    }

    public static <T> Page<T> defaultPage() {
        int pageSize = 20;
        int pageNo = 1;
        HttpServletRequest request = HttpServletUtil.getRequest();
        String pageSizeString = request.getParameter("pageSize");
        if (ObjectUtil.isNotEmpty(pageSizeString)) {
            pageSize = Integer.parseInt(pageSizeString);
        }

        String pageNoString = request.getParameter("pageNo");
        if (ObjectUtil.isNotEmpty(pageNoString)) {
            pageNo = Integer.parseInt(pageNoString);
        }

        return new Page((long)pageNo, (long)pageSize);
    }

    public static <T> Page<T> defaultPage(BaseRequest baseRequest) {
        int pageSize = 20;
        int pageNo = 1;
        if (ObjectUtil.isNotEmpty(baseRequest)) {
            pageNo = baseRequest.getPageNo() == null ? pageNo : baseRequest.getPageNo();
            pageSize = baseRequest.getPageSize() == null ? pageNo : baseRequest.getPageSize();
        }

        return new Page((long)pageNo, (long)pageSize);
    }
}
